package com.wufun.baselibrary.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.wufan.base.baselibrary.R;
import com.wufun.baselibrary.impl.IInit;
import com.wufun.baselibrary.widgets.WaitingDialog;

/**
 * 描述
 * 作者 tangbingliang
 * 时间 16/6/28 15:33
 * 邮箱 bltang@atman.com
 * 电话 18578909061
 */
public class BaseAppCompatActivity extends AppCompatActivity implements IInit {

    public WaitingDialog mWaitingDialog = null;
    private boolean isFirstInto = true;//该标志位表示第一次进入先初始化init(包含基本数据和网络获取数据)
    public boolean isRefreshNetworkData = false;//是否返回界面时刷新数据 默认不刷新
    private static long lastClickTime = 0;
    //应用是否销毁标志
    protected boolean isDestroy;
    private Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏bar
        super.onCreate(savedInstanceState);
        isDestroy=false;
        // 添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
//        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            super.startActivityForResult(intent, requestCode, options);
        } else {
            super.startActivityForResult(intent, requestCode);
        }
//        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstInto) {
            init();
            isFirstInto = false;
        }
        if (isRefreshNetworkData && !isFirstInto) {
            resumeToRefreshBaseData();
        }
    }

    /**
     * 初始化
     */
    protected void init() {
        initIntentAndMemData();
        initWidget();
        doInitBaseHttp();
    }

    /**
     * 该方法会重新刷新doInitBaseHttp中所请求的数据
     */
    protected void resumeToRefreshBaseData() {
        doInitBaseHttp();
    }

//    /**
//     * 防止快速点击,启动多个同样的界面
//     *
//     * @return
//     */
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            if (isFastDoubleClick()) {
//                return true;
//            }
//        }
//        return super.dispatchTouchEvent(ev);
//    }
//
//    /**
//     * 防止快速点击,启动多个同样的界面
//     *
//     * @return
//     */
//    public boolean isFastDoubleClick() {
//        long now = System.currentTimeMillis();
//        long timeD = now - lastClickTime;
//        lastClickTime = now;
//        return timeD <= 500;
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroy=true;
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void finish() {
        super.finish();
//        overridePendingTransition(0, R.anim.slide_right_out);
    }

    public void exitApp(){
        AppManager.getAppManager().AppExit(this);
    }

    /**
     * 开启加载Loading
     */
    public void showLoading(String str) {
        showLoading(str, true);
    }

    public void showLoading(String str, boolean isCancelable) {
        if (mWaitingDialog == null) {
            mWaitingDialog = new WaitingDialog(this, str);
        }
        mWaitingDialog.setCanceledOnTouchOutside(isCancelable);
        mWaitingDialog.startAnimation();
        mWaitingDialog.setCancelable(isCancelable);
        if (mWaitingDialog.isShowing()) {
            return;
        }
        if (!isFinishing()) {
            try {
                mWaitingDialog.show();
            } catch (Exception e) {

            }
        }
    }

    /**
     * 关闭加载Loading
     */
    public void cancelLoading() {
        try {
            if (mWaitingDialog != null && mWaitingDialog.isShowing()) {
                mWaitingDialog.stopAnimation();
                mWaitingDialog.dismiss();
            }
        } catch (Exception e){
        }
    }

    public void showToast(String text) {
        showToast(text, Toast.LENGTH_SHORT);
    }

    public void showToast(String text, int time) {
        if(mToast == null) {
            mToast = Toast.makeText(getApplicationContext(), text, time);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    public void onBackPressed() {
        cancelToast();
        super.onBackPressed();
    }

    @Override
    public void initIntentAndMemData() {}

    @Override
    public void doInitBaseHttp() {}

    @Override
    public void initWidget(View... v) {}

    @Override
    public void childChangDisplayModel() {

    }
}
